/*
 @Date: 2022/4/29
 @Desc: 首页视图控制器
 */

import UIKit
import DZNEmptyDataSet

class IPHomeViewController: UIViewController, TZImagePickerControllerDelegate, UINavigationControllerDelegate {
    var imagePickerVc:TZImagePickerController!
    var selectInt = 0
    
    @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
    @IBOutlet weak var collectionView: UICollectionView!
    private lazy var mDataHandler = IPHomeDataHandler()
    
    @IBOutlet weak var searchBar: UISearchBar!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        setupData()
    }


}

//MARK: UICollectionViewDataSource UICollectionViewDelegate
extension IPHomeViewController: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return mDataHandler.setcionNum()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return mDataHandler.cellCount(section)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let obj = mDataHandler.fetchSectionData(indexPath.section)
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: obj.cellReuseId, for: indexPath) as! IPHomeItemCell
        cell.titleLab.text = ["一寸","二寸","小一寸","小二寸","大一寸","大二寸"][indexPath.row]
        cell.subTitleLab.text = ["295x413","413x579","259x377","413x531","389x566","413x625"][indexPath.row] + "px"
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        //
        let obj = mDataHandler.fetchSectionData(indexPath.section)
        let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: obj.headerReuseId, for: indexPath)
        if reusableView.isKind(of: IPHomeTitleReusableView.self) {
            let v = reusableView as! IPHomeTitleReusableView
            v.didClickMore = {[weak self] in
                print("点击更多")
            }
        }
        return reusableView
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //todo: 点击去拍照
        //test
//        let vc = IPEditPhotoViewController()
//        show(vc, sender: nil)
        selectInt = indexPath.item
        //maxImagesCount:最多可以选择几张图片
         let vc = TZImagePickerController(maxImagesCount: 1, delegate: self)!
         vc.allowTakeVideo = false //是否允许拍视频
         vc.allowPickingVideo = false //是否允许选择视频
//         vc.allowCrop = true //是否裁剪
//         vc.needCircleCrop = true //是否带边框裁剪
        vc.circleCropRadius = Int((UIScreen.main.bounds.size.width - 60)/2)
         vc.modalPresentationStyle = .fullScreen
         self.present(vc, animated: true, completion: nil)
         //选中图片后做相应的处理
//         vc.didFinishPickingPhotosHandle = { photos,assets,isSelectOriginalPhoto in
//            if let imgs = photos {
//                if imgs.count > 0 {
//                   //拿到图片 做相应的处理
//
//                 } else {
//                   //弹框提示用户  图片选择出错，请重新选择
//
//                  }
//                }
//           }
    }
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool) {
        let vc = IPEditPhotoViewController()
        vc.chiCunStr = ["295x413","413x579","259x377","413x531","389x566","413x625"][selectInt]
        vc.selectImg = photos.first!
//        vc.iconIV.image?.size.width = 200
        show(vc, sender: nil)
    }
}
//- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto;
//MARK: UICollectionViewDelegateFlowLayout
extension IPHomeViewController: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        let obj = mDataHandler.fetchSectionData(section)
        return obj.headerSize
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let obj = mDataHandler.fetchSectionData(indexPath.section)
        return obj.itemSize
    }
}

//MARK: DZNEmptyDataSetSource
extension IPHomeViewController: DZNEmptyDataSetSource {
    func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let str = "空空如也～"
        let dict: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 16),.foregroundColor: UIColor.ip_text_color]
        return NSAttributedString.init(string: str, attributes: dict)
    }
}

//MARK: ui的一些配置
private extension IPHomeViewController {
    
    func setupUI(){
        configureCollectionView()
        registerUI()
    }
    
    func configureCollectionView(){
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        
        collectionView.backgroundColor = UIColor.systemGroupedBackground
    }
    
    func registerUI(){
        let nib = UINib.init(nibName: "IPHomeHeaderReusableView", bundle: .main)
        collectionView.register(nib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: IPHomeHeaderReusableView.reuseId)
        
        let titleNib = UINib.init(nibName: "IPHomeTitleReusableView", bundle: .main)
        collectionView.register(titleNib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: IPHomeTitleReusableView.reuseId)
        
        let cellNib = UINib.init(nibName: "IPHomeItemCell", bundle: .main)
        collectionView.register(cellNib, forCellWithReuseIdentifier: IPHomeItemCell.reuseId)
    }
}

//MARK: 数据的一些订阅
private extension IPHomeViewController {
    
    func setupData(){
        mDataHandler.reloadDataBlock = {[weak self] in
            self?.collectionView.reloadData()
        }
        mDataHandler.setupData(UIScreen.main.bounds.width)
    }
    
}
